Ext.ns('Tm.company');
Tm.company.CompanyAddPanel = Ext.extend(Ext.Panel, {
			id : 'companyAddPanel',
			frame : true,
			autoWidth : true,
			autoHeight : true,
			layout : 'fit',
			dataId : null,
			initComponent : function() {
				var dataId=this.dataId;
	
				var toolBar = new Ext.Toolbar({
							hideBorders : true,
							buttons : [{
										text : '保存',
										iconCls : 'act_acceptIcon',
										handler : function() {
											if (form.getForm().isValid()) {
												form.getForm().submit({
															waitMsg : '正在提交,请稍候...',
															url : 'company/saveOrUpdateCompany.do',
															method : 'POST',
															success : function(form, action) {
																App.setAlert('ok', '操作成功');
																var companyAddPanel = Ext.getCmp('companyAddPanel');
																var form = companyAddPanel.mainpanel.form;
																var returnCompany = Ext.decode(action.response.responseText);
																console.log(returnCompany);
																companyAddPanel.init(returnCompany,form);
																if(!companyAddPanel.mainpanel.companyAddPanel){
														
																	var companyContactorPanel = new Tm.company.CompanyContactorPanel({
																		title : '联系人管理',
																		ref : 'companyContactorPanel',
																		compId : returnCompany.id

																	});
																	companyAddPanel.mainpanel.add(companyContactorPanel);
																	companyAddPanel.mainpanel.setActiveTab(companyContactorPanel);

																
																}
																
														
															},
															failure : function(action, response) {
																App.setAlert('error', '操作失败');
															}
														});

											}
										}
									}, {
										xtype : 'tbseparator'
									}, {
										text : '重置',
										iconCls : 'act_resetIcon',
										handler : function() {
											form.getForm().reset();

										}
									} ]
						});

				var form = new Tm.common.FormPanel({
							title : '基本信息',
							ref : 'form',
							labelWidth : 100,
							defaults : {
								xtype : 'textfield',
								margins : '0 5 0 0',
								width : 300
							},
							errorReader : new Tm.common.DataReader(),
							tbar : toolBar,
							items : [{
										name : 'id',
										hidden : true
									},{
										fieldLabel : '客户编码',
										name : 'code',
										allowBlank : false
									}, {
										fieldLabel : '客户名称',
										name : 'name',
										allowBlank : false
									}, {

										fieldLabel : '客户类型',
										name : 'type',
										xtype : 'combo',
										editable : false,
										typeAhead : true,
										forceSelection : true,
										hiddenName : 'type',
										mode : 'local',
										triggerAction : 'all',
										emptyText : '请选择...',
										selectOnFocus : true,
										store : Tm.data.companyTypes,
										allowBlank : false
									}, {
										fieldLabel : '电话',
										name : 'tel',
										allowBlank : true
									}, {
										fieldLabel : '传真',
										name : 'fax',
										allowBlank : true
									}, {
										fieldLabel : '网址',
										name : 'website',
										allowBlank : true
									}

							]
						});

				


						
	
				
				var mainpanel = new Ext.TabPanel({
							ref : 'mainpanel',
							height : 625,
							width : 600,
							activeTab : 0,
							defaults : {
								autoScroll : true
							},// 自动出现滚动条
							items : [form],

							enableTabScroll : true
						});
				// mainpanel.setActiveTab('companymainpanel');

				Ext.apply(this, {
							items : [mainpanel]
						});
				Tm.company.CompanyAddPanel.superclass.initComponent.call(this);
			},
			loadData : function() {
				var dataId = this.dataId;
				if (dataId) {
					Ext.Ajax.request({
								url : 'company/loadCompany.do',
								params : {
									id : dataId
								},
								success : function(response, action) {
									var data = Ext.decode(response.responseText);
									var companyAddPanel = Ext.getCmp("companyAddPanel");
									var form = companyAddPanel.mainpanel.form;
									companyAddPanel.init(data,form);
									var companyContactorPanel = new Tm.company.CompanyContactorPanel({
										title : '联系人管理',
										ref : 'companyContactorPanel',
										compId : dataId
										//disabled : true,
										//compId :dataId							
									});
									companyAddPanel.mainpanel.add(companyContactorPanel);
								},
								failure : function(response, action) {
									App.setAlert('error', '载入失败');
								}

							});
				}
			},
			init : function(data,form) {

				form.getForm().findField("id").setValue(data.id);
				form.getForm().findField("name").setValue(data.name);
				form.getForm().findField("type").setValue(data.type);
				form.getForm().findField("code").setValue(data.code);
				form.getForm().findField("tel").setValue(data.tel);
				form.getForm().findField("fax").setValue(data.fax);
				form.getForm().findField("website").setValue(data.website);

				
			},
			listeners : {
				render : function() {
					this.loadData();
				}
			}
		});
